const app = getApp()
const util = require('../util/util.js')

Page({

  data: {
    list: [],
    page: 1,
  },

  onLoad: function(options) {
    app.editTabbar()
    this.getListByPage(this.data.page)
  },

  onReachBottom: function() {
    this.getListByPage(this.data.page)
  },

  onShareAppMessage: function() {
    var taskList = util.getTaskList()
    if (!taskList.share) {
      taskList.share = 1
      util.setTaskList(taskList)
    }
  },

  getListByPage: function(page) {
    var that = this
    // 获取列表
    wx.request({
      url: app.globalData.url + 'find?page=' + page,
      success: function(res) {
        // console.log(res)
        var mlist = res.data.data
        if (mlist.length != 0) {
          that.setData({
            list: that.data.list.concat(mlist),
            page: that.data.page + 1
          })
        } else {
          wx.showToast({
            title: '已经到底了~',
            icon: 'none',
            image: '',
            duration: 1500,
            mask: false,
          })
        }
      }
    })
  },

  // 跳转小程序(判断是否有二维码图片,有则展示)
  goMiniProgram: function(e) {
    var game = e.currentTarget.dataset.game
    var list = wx.getStorageSync('baibao_game_history')
    list.push(game)
    wx.setStorageSync('baibao_game_history', list)
    if (game.click_type == 2) {
      wx.previewImage({
        current: game.code_url,
        urls: [game.code_url]
      })
    } else if (game.click_type == 1) {
      // console.log(game)
      wx.navigateToMiniProgram({
        appId: game.appid,
        path: game.path,
        extraData: game.extra_data,
        success(res) {
          // 添加最近玩过
        }
      })
    } else if (game.click_type == 3) {
      wx.navigateToMiniProgram({
        appId: game.belong,
        path: game.path,
        extraData: game.extra_data,
        success(res) {}
      })
    }
  },
})